package cn.itcast.utils;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;

public class WebUtils {                                           //RegistForm.class  
	public static <T> T requestToBean(HttpServletRequest request, Class<T> beanClass)   {
		
		try {
			T bean = beanClass.newInstance();
			
			Enumeration e = request.getParameterNames();
			while(e.hasMoreElements()) {
				String name = (String)e.nextElement();
				String value = request.getParameter(name);
				
				BeanUtils.setProperty(bean, name, value);
				
			}
			return bean;
		} catch (Exception e) {
			throw new RuntimeException(e);
		} 
		
	
	}
	
	public static void copyBean(Object src, Object dest) {
		
		ConvertUtils.register(new Converter(){
			public Date convert(Class type, Object value) {
				if(value == null) {
					return null;
				}
				String str = (String)value;
				if(str.trim().equals("")) {
					return null;
				}
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
				try {
					return df.parse(str);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					throw new RuntimeException(e);
				}
				
			}
		}, Date.class);
		
		
		 
		try {
			BeanUtils.copyProperties(dest, src);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}catch (IllegalArgumentException e) {
			throw new RuntimeException(e);
		}
		 
	}
	//产生全球唯一ID
	public static String generateID() {
		return UUID.randomUUID().toString();
	}
}
